package com.zking.dao;

import com.zking.entity.Address;

import java.sql.SQLException;
import java.util.List;

public class AddressDao extends BaseDaoImpl {

    /**
     * 按用户名查询
     *
     * @return
     */
    public List<Address> queryStudents(String username) {
        String sql = "select * from address where username= '"+username+"';";
        try {
            return findList(Address.class, sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 插入一条数数据
     *
     * @param address
     * @return
     */
    public int insertStudent(Address address) {

        String sql = "insert into `rz1217db_2`. `address` (`name`, `address`, `phone`, `username`) values(?,?,?,?)";
        //INSERT INTO `rz1217db_2`.`address`(`name`, `address`, `phone`, `username`) VALUES ('三廖全景', '阿斯顿', 123, '12')
        Object[] params = {address.getName(), address.getAddress(), address.getPhone(), address.getUsername()};
        try {
            return update(sql, params);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 修改数据
     *
     * @param address
     * @return
     */
    public int updateStudent(Address address) {
        String sql = "UPDATE `address` SET " + "`name` = ?, `phone` = ? WHERE `address` = ?";
        Object[] params = {address.getName(), address.getPhone(), address.getAddress()};
        try {
            return update(sql, params);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 删除数据
     *
     * @return
     */
    public int DeleteDddress(Address address){
        String sql ="DELETE FROM `rz1217db_2`.`address` WHERE `name` = ? AND `address` = ? AND `phone` = ? AND `username` = ?";
        Object[] params = {address.getName(), address.getAddress(), address.getPhone(),address.getUsername()};
        try {
            return  runner.update(sql,params);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }
    /*
        修改地址
     */
    public int updateAddress(Address address,Address address1) {
        String sql = "UPDATE `rz1217db_2`.`address` SET `name` = ?, `address` = ?, `phone` = ?, `username` = ? WHERE `name` = ? AND `address` = ? AND `phone` = ? AND `username` = ? ";
       // UPDATE `rz1217db_2`.`address` SET `name` = '静安大火的话', `address` = '啊回答后', `phone` = '15675276365' WHERE `name` = '阿斯弗' AND `address` = '公司给对方卡接口' AND `phone` = '18508402409' AND `username` = '654321'
        Object[] params = {address.getName(),address.getAddress(),address.getPhone(),address.getUsername(),address1.getName(),address1.getAddress(),address1.getPhone(),address1.getUsername()};
        try {
            return update(sql, params);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }
}
